var vm = new Vue({
    el: '#rrapp',
    data: {
        kpiTargetList: [
            {
                value: 'MTTR',
                label: 'MTTR'
            },
            {
                value: 'MTBF',
                label: 'MTBF'
            },
            {
                value: 'MJGZL',
                label: '模具故障率'
            }
        ],
        deviceOrganizationList: [],
        q: {
            moldCode: [], // 模具编码列表，查询用
            yearly: ''
        },
        moldNoList: [],
        deviceOrganization: '江北发动机NE1工厂' // 设备组织名称
    },
    methods: {
        loadAndRenderKpiView: function () {
            var _this = this;
            var param = {
                targetList: ['MTTR', 'MTBF', 'MJGZL']
            };
            // KPI查询年度
            if (_this.q.yearly) {
                param.yearly = new Date(_this.q.yearly).getFullYear();
            } else {
                param.yearly = new Date().getFullYear();
            }
            if(_this.q.moldCode && _this.q.moldCode.length > 0){
                param.moldCode = _this.q.moldCode
            }

            // 加载设备组织列表
            Ajax.request({
                url: "../kpi/mold/stat",
                params: JSON.stringify(param),
                type: "POST",
                contentType: "application/json",
                successCallback: function (res) {
                    _this.renderKpiView(res.kpiOptions);
                }
            });
        },
        reset: function(){
            // 重置检索条件
            this.q = {
                yearly: '',
                moldCode:[] // 模具编码列表，查询用
            };
            this.moldNoList = [];
        },
        renderKpiView: function (kpiOptionList) {
            $('#main').empty()
            for (let i = 0; i < this.kpiTargetList.length; i++) {
                let kpiTarget = this.kpiTargetList[i];
                // 绘制新的图表DIV
                let chartDomStr = '<div style="width: 50%;height: 60%;" id="' + kpiTarget.value + '"></div>'
                $('#main').append(chartDomStr)
                let kpiOption = kpiOptionList[i];
                let option = {
                    title: {
                        text: kpiTarget.label,
                        left: 'center'
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        type : 'scroll',
                        top : '10%',
                        data: kpiOption.legendData
                    },
                    grid: {
                        left: '2%',
                        right: '4%',
                        top: '22%',
                        containLabel: true
                    },
                    toolbox: {
                        show: true,
                        feature: {
                            dataView: {show: true, readOnly: false},
                            magicType: {show: true, type: ['line', 'bar']},
                            restore: {show: true},
                            saveAsImage: {show: true}
                        }
                    },
                    calculable: true,
                    xAxis:{
                        type: 'category',
                        data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月']
                    },
                    yAxis: kpiOption.barChartYAxisOptionModel,
                    series: kpiOption.barSeriesData
                };
                echarts.init(document.getElementById(kpiTarget.value)).setOption(option);
            }
        },
        getModuleArchives: function () {
            openWindow({
                type: 2,
                title: '<i class="fa fa-file-text-o"></i> 选择模具',
                content: '../module/moduleArchiveSelect.html',
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var iframeWin = window["layui-layer-iframe" + index];
                    var res = iframeWin.getSelectedModuleArchivesList();
                    if (!res) {
                        iview.Message.error("请选择一条记录");
                    } else {
                        // 清空模具编码和模具号列表
                        vm.q.moldCode = [];
                        vm.moldNoList = [];
                        for (let i = 0; i < res.length; i++) {
                            vm.q.moldCode.push(res[i].moduleCode);
                            vm.moldNoList.push(res[i].moduleNo)
                        }
                        layer.close(index);
                    }
                }
            })
        }
    },
    mounted: function () {
        this.$Message.info('请选择要查询的模具和统计年份');
    }
});
